﻿using FreeSql;

namespace Wen.Core.Extensions;

public static class FreeSqlHelpExtension
{
    /// <summary>
    /// 插入数据 Object
    /// </summary>
    /// <param name="fsql"></param>
    /// <param name="item"></param>
    /// <returns></returns>
    public static IInsert<object> Insrt(this IFreeSql fsql, object item)
    {
        return fsql.Insert<object>().AsType(item.GetType()).AppendData(item);
    }

    /// <summary>
    /// 插入数据 泛型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="fsql"></param>
    /// <param name="item"></param>
    /// <returns></returns>
    public static IInsert<T> Insert<T>(this IFreeSql fsql, T item) where T : class
    {
        return fsql.Insert<T>().AppendData(item);
    }

    /// <summary>
    /// 删除数据 Object
    /// </summary>
    /// <param name="fsql"></param>
    /// <param name="item"></param>
    /// <returns></returns>
    public static IDelete<object> Delete(this IFreeSql fsql, object item)
    {
        return fsql.Delete<object>().AsType(item.GetType()).Where(item);
    }

    /// <summary>
    /// 删除数据 泛型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="fsql"></param>
    /// <param name="item"></param>
    /// <returns></returns>
    public static IDelete<T> Delete<T>(this IFreeSql fsql, T item) where T : class
    {
        return fsql.Delete<T>().Where(item);
    }

    /// <summary>
    /// 更新数据 Object
    /// </summary>
    /// <param name="fsql"></param>
    /// <param name="item"></param>
    /// <returns></returns>
    public static IUpdate<object> Update(this IFreeSql fsql, object item)
    {
        return fsql.Update<object>().AsType(item.GetType()).SetSource(item);
    }

    /// <summary>
    /// 更新数据 泛型
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="fsql"></param>
    /// <param name="item"></param>
    /// <returns></returns>
    public static IUpdate<T> Update<T>(this IFreeSql fsql, T item) where T : class
    {
        return fsql.Update<T>().SetSource(item);
    }

    /// <summary>
    /// 查询数据 Object
    /// </summary>
    /// <param name="fsql"></param>
    /// <param name="itemType"></param>
    /// <returns></returns>
    public static ISelect<object> Select(this IFreeSql fsql, Type itemType)
    {
        return fsql.Select<object>().AsType(itemType);
    }

    /// <summary>
    /// 重命名 执行SQL语句，返回影响的行数
    /// 叶老板吐槽命名更好，先做个拓展备用
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="insert"></param>
    /// <returns></returns>
    public static int ExecuteEffects<T>(this IInsert<T> insert) where T : class
    {
        return insert.ExecuteAffrows();
    }

    /// <summary>
    /// 重命名 执行SQL语句，返回影响的行数
    /// 叶老板吐槽命名更好，先做个拓展备用
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="insert"></param>
    /// <returns></returns>
    public static int ExecuteEffects<T>(this IUpdate<T> insert) where T : class
    {
        return insert.ExecuteAffrows();
    }
    /// <summary>
    /// 重命名 执行SQL语句，返回影响的行数
    /// 叶老板吐槽命名更好，先做个拓展备用
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="insert"></param>
    /// <returns></returns>
    public static int ExecuteEffects<T>(this IDelete<T> insert) where T : class
    {
        return insert.ExecuteAffrows();
    }

    /// <summary>
    /// 分页查询 返回查询数据 和数量
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="select"></param>
    /// <param name="pageNumber"></param>
    /// <param name="pageSize"></param>
    /// <returns></returns>
    public static (List<T> Items, int Count) ToPageList<T>(this ISelect<T> select, int pageNumber, int pageSize)
    {
        var sel = select.Page(pageNumber, pageSize);
        var count = sel.Count();
        var items = sel.ToList();
        return (items, (int)count);
    }
}
